package ru.ifmo.ipimenov.ewf_svn.standalone.view.gui;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ru.ifmo.ipimenov.ewf_svn.standalone.controller.*;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.UserProvider;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.list.Model;

import javax.swing.*;
import java.awt.*;

/**
 * @author: Ilya Pimenov
 * Scartel Starlab company
 */
public class MainForm extends JFrame {
    /**
     * Logger for all purposes
     */
    private static Log log = LogFactory.getLog(MainForm.class);

    private JButton acquireButton;
    private JButton processButton;
    private JButton deployButton;
    private JButton addButton;
    private JButton stateButton;
    private JButton ruleButton;

    private JList list;

    private JLabel labelUp;

    private JLabel userLabel;

    private Container contentPane;

    public MainForm() {        
        super("Document Work Flow");

        setPreferredSize(new Dimension(500, 300));

        int yLocation = (int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2 - getPreferredSize().getHeight() / 2);
        int xLocation = (int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2 - getPreferredSize().getWidth() / 2);
        setLocation(xLocation, yLocation);


        list = new JList(new String[]{"empty"});

        acquireButton = new JButton("<html><span style=\"width: 200px;\">Acquire documents</span></html>");
        acquireButton.addActionListener(new AcquireAction(list));

        processButton = new JButton("<html><span style=\"width: 200px;\">Process document</span></html>");
        processButton.addActionListener(new ProcessAction(list));

        deployButton = new JButton("<html><span style=\"width: 200px;\">Deploy document</span></html>");
        deployButton.addActionListener(new DeployAction(list));

        addButton = new JButton("<html><span style=\"width: 200px;\">Add Document</span></html>");
        addButton.addActionListener(new AddAction(list));

        stateButton = new JButton("<html><span style=\"width: 200px;\">Set State</span></html>");
        stateButton.addActionListener(new StateAction(list));

        ruleButton = new JButton("<html><span style=\"width: 200px;\">Manage Rules</span></html>");
        ruleButton.addActionListener(new RuleAction(list));

        labelUp = new JLabel("empty");

        userLabel = new JLabel("<html>Logged as <span style=\"color: #0000FF; font-size: 14pt; text-decoration: underline;\">" + UserProvider.getInstance().getUser().getName() + "</span></html>");

        contentPane = getContentPane();
        addComponentsToPane();
    }

    private void addComponentsToPane() {
        Box box = new Box(BoxLayout.Y_AXIS);

        box.add(acquireButton);
        box.add(processButton);
        box.add(deployButton);
        box.add(addButton);
        box.add(stateButton);
        box.add(ruleButton);

        JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new BorderLayout(2, 2));
        infoPanel.add(labelUp, BorderLayout.PAGE_START);

        contentPane.setLayout(new BorderLayout(5, 5));

        if (!(contentPane.getLayout() instanceof BorderLayout)) {
            contentPane.add(new JLabel("Container doesn't use BorderLayout."));
            return;
        }

        contentPane.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);

        contentPane.add(box, BorderLayout.LINE_START);

        list.setPreferredSize(new Dimension(300, 200));
        list.setSelectedIndex(0);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        contentPane.add(new JScrollPane(list), BorderLayout.CENTER);
        list.addListSelectionListener(new DocumentListSelectionListener(labelUp, list));

        contentPane.add(infoPanel, BorderLayout.PAGE_END);
        contentPane.add(userLabel, BorderLayout.PAGE_START);
    }
}
